package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.annotations.IgnoreClientToken;
import com.bwda.dsrs.filecenter.aspect.LogTypeEnum;
import com.bwda.dsrs.filecenter.aspect.Loggable;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.form.terminalversion.TerminalVersionCreateForm;
import com.bwda.dsrs.filecenter.domain.po.terminal.TerminalPo;
import com.bwda.dsrs.filecenter.domain.condition.terminal.TerminalCondition;
import com.bwda.dsrs.filecenter.domain.form.terminal.TerminalQueryForm;
import com.bwda.dsrs.filecenter.domain.form.terminal.TerminalUpdateForm;
import com.bwda.dsrs.filecenter.domain.vo.terminal.TerminalVo;
import com.bwda.dsrs.filecenter.service.TerminalService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;

import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;

import com.bwda.dsrs.filecenter.service.TerminalVersionService;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 终端
 * @author yangjun
 */
@RestController
@RequestMapping("/b/terminal")
@Api(value = "/b/terminal", tags = "")
public class TerminalController extends BaseController {

	@Autowired
	private TerminalService terminalService;

	@Autowired
	private TerminalVersionService terminalVersionService;

	@ApiOperation(value = "查询终端权限",notes = "查询",httpMethod = "GET")
	@Loggable(descp = "查询终端权限",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryQx", method = {RequestMethod.GET, RequestMethod.POST})
	@IgnoreClientToken
	public ResponseEntity<String> query(@ModelAttribute@Valid TerminalQueryForm form) throws BusinessServiceException {
		TerminalPo po = terminalService.selectByNo(form.getTerminalCode());
		if (po==null){
			return getSuccessResult("终端未注册或被删除");
		}
		String[] ips= form.getIp().split(",");
		po.setIp(ips);
		//判断权限
		terminalService.isJurisdiction(po);
		return getSuccessResult("操作成功","true");
	}
	@ApiOperation(value = "查询终端数量",notes = "查询数量",httpMethod = "GET")
	@Loggable(descp = "查询终端数量",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid TerminalQueryForm form) throws BusinessServiceException {
		TerminalCondition condition = this.getConditionByQueryForm(form);
		int count = terminalService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询终端列表",notes = "查询列表",httpMethod = "GET")
	@Loggable(descp = "查询终端列表",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<TerminalVo>> queryList(@ModelAttribute@Valid TerminalQueryForm form) throws BusinessServiceException {
		TerminalCondition condition = this.getConditionByQueryForm(form);
		List<TerminalPo> poList = terminalService.queryList(condition);
		List<TerminalVo> voList = CopyUtil.transfer(poList, TerminalVo.class);
		return getSuccessResult(getListResponse(voList));
	}
	@ApiOperation(value = "查询终端列表(带分页)",notes = "查询列表(带分页)",httpMethod = "GET")
	@Loggable(descp = "查询终端列表(带分页)",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<TerminalVo>> queryPageList(@ModelAttribute@Valid TerminalQueryForm form) throws BusinessServiceException {
		form.setTerminalVersion(form.getTerminalVersion().toUpperCase());
		form.setMacAddr(form.getMacAddr().toUpperCase());
		TerminalCondition condition = this.getConditionByQueryForm(form);
		List<TerminalVo> voList = new ArrayList<>();
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		int count = terminalService.queryCount(condition);
		if (count > 0) {
			List<TerminalPo> poList = terminalService.selectTlist(condition);
			voList = CopyUtil.transfer(poList, TerminalVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增终端",notes = "新增",httpMethod = "POST")
	@Loggable(descp = "新增终端",type = LogTypeEnum.CREATE,include = "")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	@IgnoreClientToken
	public ResponseEntity<String> add(@ModelAttribute@Valid TerminalVersionCreateForm form) throws BusinessServiceException {
		TerminalPo po1 = new TerminalPo();
		po1.setTerminalVersion(form.getCurrentTerminalVersion());
		po1.setMacAddr(form.getMacAddr());
		//判断mac地址是否是第一次注册
		TerminalPo terminalPo = terminalService.selectByMac(form.getMacAddr());
		if (terminalPo==null){
			po1.setTerminalState(BusinessConstants.NumberConstant.ZERO);
			po1.setTerminalCode(terminalService.getNextVal().toString());
			terminalService.insert(po1);
		}else {
			terminalService.update(po1);
		}
		//返回终端编码
		TerminalPo terminalPo1 = terminalService.selectByMac(form.getMacAddr());
		return getSuccessResult("操作成功",terminalPo1.getTerminalCode());
	}
	@ApiOperation(value = "修改终端",notes = "修改",httpMethod = "POST")
	@Loggable(descp = "修改终端",type = LogTypeEnum.UPDATE,include = "")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid TerminalUpdateForm form) throws BusinessServiceException {
		TerminalPo po = CopyUtil.transfer(form, TerminalPo.class);
		terminalService.update(po);
		return getSuccessResult();
	}
	@ApiOperation(value = "删除终端",notes = "删除",httpMethod = "POST")
	@Loggable(descp = "删除终端",type = LogTypeEnum.DELETE,include = "")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "终端编号", required = true)@RequestParam String id) throws BusinessServiceException {
		terminalService.delete(id);
		return getSuccessResult();
	}

	/**
	 * TerminalQueryForm转换为TerminalCondition
	 *
	 * @param form
	 * @return
	 */
	private TerminalCondition getConditionByQueryForm(TerminalQueryForm form) {
		TerminalCondition condition = CopyUtil.transfer(form, TerminalCondition.class);
		return condition;
	}

}